# #####################################正数在负数前，整数从小到大，负数从大到小
list1 = [7, -8, 5, 4, 0, -2, -5]
list2 = sorted(list(filter(lambda x: x < 0, list1)), reverse=True)
list3 = sorted(list(filter(lambda x: x >= 0, list1)))
print(list2+list3)


# ####################################小写<大写<奇数<偶数
s = 'asdf234GDSdsf23'
s2 = "".join(sorted(s, key=lambda x: (x.isdigit(), x.isdigit() and int(x) % 2 == 0, x.isupper(), x)))
print(s2)


# ####################################map判断合法用户名
import re
username = ["123", "abc", "_abc123", "123abc"]
def check(x):
    if re.findall("^[A-Za-z_]\w{5,17}$", x):
        print(f"{x}:符合要求！")
    else:
        print(f"{x}:不符合要求！")
        return 0
print(list(map(check, username)))


# ####################################filter把合法的用户显示出来
import re
username = ["123", "abc", "_abc123", "123abc"]
def check(x):
    if re.findall("^[A-Za-z_]\w{5,17}$", x):
        return 1
    else:
        return 0
print(list(filter(check, username)))


# ###################################匿名函数+reduce实现1*2*3*...*n
from functools import reduce
n = 10
print(reduce(lambda x,y: x * y,range(1,n)))
# filter函数：找出[1-100]能被3整除的数
print(list(filter(lambda x: x % 3 == 0, range(1,100))))


# ##################################map函数：求list[1-10]各个项的平方
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
print(list(map(lambda x: x**2, list1[0:10])))


# #################################将列表[1, 4, 5, 9]转换成'1459'的字符串
from functools import reduce
list2 = [1, 4, 5, 9]
print(reduce(lambda x, y: str(x) + str(y), list2))


###################################查找文件中最长的行，输出长度
import string
import random
with open('big.data', 'w+', encoding="utf-8")as f:
    f.write("".join(random.choice(string.ascii_lowercase, k = random.randint(1, 50))) + "\n").encode("utf-8"))